const common = {

}

common.calculationHeight = function (num) {
    return (num / 750) * window.screen.width;
}
common.isExist = function (cart, commodity) {
    var index = -1;
    for (var i = 0; i < cart.commodityList.length; i++) {
        if (cart.commodityList[i].commodityId == commodity.commodityId) {
            index = i;
            break;
        }
    }
    return index;
}
common.isExistById = function (cart, commodity) {
    var count = 0;
    for (var i = 0; i < cart.commodityList.length; i++) {
        if (cart.commodityList[i].commodityId == commodity.commodityId) {
            count += cart.commodityList[i].count
        }
    }
    return count;
}
common.getAllPrice = function (list) {
    let allPrice = 0;
    for (var i = 0; i < list.length; i++) {
        if (list[i].selected) {
            allPrice =
                parseFloat(allPrice) +
                parseFloat(list[i].price) *
                list[i].count;
        }
    }
    return allPrice;
}
common.getSelectedCommodities = function (list) {
    let commodities = [];
    for (var i = 0; i < list.length; i++) {
        if (list[i].selected) {
            commodities.push(list[i])
        }
    }
    return commodities;
}
// 根据商品的 数量 在 价格区间 内获得正确的 商品单价
common.getPrice = function (commodity) {
    console.log("getPrice in",commodity.purchase)
    var price = 0;
    //商品的数量
    var purchase = parseInt(commodity.purchase)
    for (let index = 0; index < commodity.priceList.length; index++) {
        const section = commodity.priceList[index];
        if (section.limit == null) {
            if (purchase > parseInt(section.min) && purchase < parseInt(section.max)) {
                return section.price;
            } else {
                continue;
            }
        } else {
            if (purchase > parseInt(section.limit)) {
                return section.price;
            }
        }
    }
    return price;
}

export default common